<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 类型逻辑层
 *
 * @author derek <693472824@qq.com>
 */
class DictionaryLogic extends BaseLogic {

    /**
     * 保存类型信息
     *
     * @param int|string $dict_id
     *            类型id
     * @author derek <693472824@qq.com>
     */
    public function addAction() {
        
        $dict = D('Dictionary');
        
        $dict_id = I('dict_id');
        
        $dictModel = $dict->create();
        
        if (! empty($dictModel)) {
            if (empty($dict_id)) {
                return $dict->add();
            } else {
                return $dict->where('dict_id=' . $dict_id)->save();
            }
        } else {
            $this->error = $dict->getError();
            return false;
        }
    }

    /**
     * 保存子类型信息
     *
     * @param int|string $dict_id
     *            子类型id
     * @author derek <693472824@qq.com>
     */
    public function addEnum() {
        
        $dictEnum = D('DictionaryEnum');
        $enum_id = I('dictEnum_id');
        $dict_id = I('dict_id');
        
        if(empty($dict_id)){
            
            $dict_id = $dictEnum->where(array('enum_id'=>$enum_id))->getField('dict_id');
        }
        
        //清空配置项缓存
        S('DICTIONARY_CONFIG_DATA', null);
        
        if (empty($enum_id)) {
            
            $maxValue = $dictEnum->where('dict_id=' . $dict_id)->max('enum_value');
            $data['sort_no'] = empty($maxValue) ? 1 : $maxValue + 1;
            $data['enum_value'] = empty($maxValue) ? 1 : $maxValue + 1;
            $data['dict_id'] = $dict_id;
            $data['enum_name'] = I('enum_name');
            
            $model = $dictEnum->create($data);
            
            if ($model) {
                
                return $dictEnum->add();
            } else {
                
                $this->error = $dictEnum->getError();
                return false;
            }
        } else {
            
            $data = array(
                    'enum_name' => I('enum_name'),
                    'sort_no' => I('sort_no')
            );
            
            return $dictEnum->where('enum_id=' . $enum_id)->setField($data);
        }
    }

    /**
     * 类型删除
     *
     * @param array|int|string $dict_id
     *            类型id
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function del ($dict_id)
    {
        $dictModel = D('Dictionary');
        
        if (is_array($dict_id)) {
            $where['dict_id'] = array(
                    'in',
                    $dict_id
            );
        } else {
            $where['dict_id'] = (string) $dict_id;
        }
        $result = $dictModel->where($where)->delete();
        $dictEnum = D('DictionaryEnum');
        $enumList = $dictEnum->where($where)->delete(); // 删除子类
        return $result;
    }

    /**
     * 子类型 删除
     *
     * @param array|int|string $enum_id
     *            子类型ID
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function delEnum ($enum_id)
    {
        $enumModel = D('DictionaryEnum');
        
        if (is_array($enum_id)) {
            $where['enum_id'] = array(
                    'in',
                    $enum_id
            );
        } else {
            $where['enum_id'] = (string) $enum_id;
        }
        
        //清空配置项缓存
        S('DICTIONARY_CONFIG_DATA', null);
        
        $dict_id = $enumModel->where($where)->getField('dict_id');
        $this->updateStyle($dict_id);
        
        $result = $enumModel->where($where)->delete();
        
        return $result;
    }

    /**
     * 根据类型ID获取单个类型
     *
     * @param int $dict_id
     *            类型ID
     *            
     * @return object $dictModel 返回用户的一个模型
     * @author derek <693472824@qq.com>
     */
    public function getDicByID ($dict_id)
    {
        if (empty($dict_id)) {
            return null;
        }
        $dict = D('Dictionary');
        $dict_where['dict_id'] = $dict_id;
        $dictModel = $dict->where($dict_where)->find();
        return $dictModel;
    }
    
    /**
     * 取配置项值
     * @return array    $config  返回数据库中配置项  array(key->value)
     * @author derek <693472824@qq.com>
     */
    public function getList(){
    
        $dict   = M('Dictionary')->getField('dict_id, dict_group, dict_name, dict_type');
        $dictEnum   = M('DictionaryEnum')->order('dict_id, sort_no, enum_id')->getField('enum_id, dict_id, enum_name, enum_value, sort_no');
    
        $config = array();
        
        foreach ($dict as $key=>$val){
            $config[$val['dict_group']] = array();
            $config[$val['dict_group']]['dict_name'] = $val['dict_name'];
            $config[$val['dict_group']]['dict_type'] = $val['dict_type'];
        }
        
        foreach ($dictEnum as $key=>$val){
            
            if(array_key_exists($val['dict_id'], $dict)){
                
                if(isset($config[$dict[$val['dict_id']]['dict_group']])){
                    
                    $config[$dict[$val['dict_id']]['dict_group']]['dict_enum'][$key] = $val;
                }
            }
        }
        
        return $config;
    }
    
}